#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int countc(string& input)
{
	return input.size();
}

int countw(string& input)
{
	int s = input.size();
	int count = 0;
	string str = input;
	for (int i = 0;i < s;i++)
	{
		if ((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122))
		{
			count++;
			while (((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122)) && str[i] != '\0')
			{
				i++;
			}
		}
	}
	return count;
}

int counts(string& input)
{
	int s = input.size();
	int count = 0;
	string str = input;
	for (int i = 0;i < s;i++)
	{
		if (str[i] == '.'|| str[i] == '!'|| str[i] == '?')
		{
			count++;
		}
	}
	return count;
}

int main(int argc, char* argv[])
{
	if (argc != 3)
	{
		cout << "输入格式为：" << endl;
		cout << "xxx.exe -c xxx.txt 统计字符数" << endl;
		cout << "xxx.exe -w xxx.txt 统计单词数" << endl;
		cout << "xxx.exe -s xxx.txt 统计句子数" << endl;
		cout << "xxx.exe -d xxx.txt 统计代码行" << endl;
		cout << "xxx.exe -k xxx.txt 统计空行" << endl;
		cout << "xxx.exe -z xxx.txt 统计注释行" << endl;
		cout << "xxx.exe -a xxx.txt 以上所有" << endl;
		return 1;
	}
	
	string option = argv[1];
	string filename = argv[2];
	ifstream file(filename);
	
	if (file.is_open() == 0)
	{
		cout << "文件打开失败" << endl;
		return 1;
	}
	
	string text1;
	string text2 = "2";
	string text;
	int h1=0,h2=0,h3=0,h,hh=0;
	while (getline(file, text1))
	{
		text += text1;
		if (text2 == "1")
		{
			h2++;
		}
		for (h = 0;h <= text1.size();h++)
		{
			if (text1[h] == '/')
			{
				if (text1[h + 1] == '/')
				{
					h3++;
				}
			}
			if (text1[h] == '/')
			{
				if (text1[h + 1] == '*')
				{
					hh=1;
				}
			}
			if (text1[h] == '*')
			{
				if (text1[h + 1] == '/')
				{
					hh = 0;
					h3++;
				}
			}
		}
		if (hh == 1)
		{
			h3++;
		}
		text2 = text1 + "1";
		h1++;
		
	}
	
	if (option == "-c")
	{
		cout << "字符数:" << countc(text) << endl;
	}
	if (option == "-w")
	{
		cout << "单词数:" << countw(text) << endl;
	}
	if (option == "-s")
	{
		cout << "句子数:" << counts(text) << endl;
	}
	if (option == "-d")
	{
		cout << "代码行:" << h1 << endl;
	}
	if (option == "-k")
	{
		cout << "空行:" << h2 << endl;
	}
	if (option == "-z")
	{
		cout << "注释行:" << h3 << endl;
	}
	if (option == "-a")
	{
		cout << "字符数:" << countc(text) << endl;
		cout << "单词数:" << countw(text) << endl;
		cout << "句子数:" << counts(text) << endl;
		cout << "代码行:" << h1 << endl;
		cout << "空行:" << h2 << endl;
		cout << "注释行:" << h3 << endl;
	}
	return 0;
}
